<template>
  <div class="login">
    <el-card class="login-card" shadow="always">
      <h3>管理员登录</h3>
      <el-form :model="loginForm" label-width="0">
        <el-form-item>
          <el-input v-model="loginForm.account" placeholder="请输入账号" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input type="password" v-model="loginForm.password" placeholder="请输入密码" prefix-icon="el-icon-lock"
            @keyup.enter.native="loginFn"></el-input>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="loginForm.checked">记住密码</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loginFn" style="width: 100%">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { adminLogin } from "../api/index"

// 隐藏文本（用于密码存储加密）
// const hide = (value) => {
//   let str = value + ""
//   let arr = str.split("")
//   let txt = ""
//   for (let i = 0; i < arr.length; i++) {
//     txt += arr[i] + "_" + i
//   }
//   return txt
// }

// 显示文本（用于密码存储解密）
// const show = (txt) => {
//   let arr = txt.split("_")
//   let value = arr[0]
//   for (var i = 1; i < arr.length; i++) {
//     value += (arr[i] + "").slice(1)
//   }
//   return value
// }

// 直接返回原始值（已注释掉加密逻辑）
const hide = (value) => {
  return value
}

// 直接返回原始值（已注释掉解密逻辑）
const show = (txt) => {
  return txt
}

export default {
  data() {
    return {
      loginForm: {
        account: "",
        password: "",
        checked: false
      }
    }
  },
  methods: {
    // 登录处理
    async loginFn() {
      let account = this.loginForm.account
      let password = this.loginForm.password
      let checked = this.loginForm.checked

      if (account == "" || password == "") {
        this.$message({
          message: "输入框不能为空",
          type: "error"
        })
        return false
      }

      await adminLogin({ account, password })
        .then(resData => {
          if (resData.code == 200) {
            let account = resData.account
            let username = resData.username
            let token = resData.token
            let role = resData.role || '1' // 默认为管理员，因为admin登录接口只允许管理员

            localStorage.setItem("AUTO_TOKEN", token)
            localStorage.setItem("USERNAME", username)
            localStorage.setItem("ACCOUNT", account)
            localStorage.setItem("USER_ROLE", role)

            this.$message({
              type: "success",
              message: "登录成功"
            })

            setTimeout(() => {
              this.$router.replace({ path: "/home" }).catch(() => { })
            }, 150)
          } else if (resData.code == -2) {
            // 账号被封禁
            this.$message({
              type: "error",
              message: resData.msg || "该账号已被封禁，请更换账号登录或联系管理员解封"
            })
          } else {
            this.$message({
              type: "error",
              message: resData.msg
            })
          }
        })
        .catch(() => {
          this.$message({
            type: "error",
            message: "登录失败，请稍后重试"
          })
        })

      if (checked) {
        localStorage.setItem("XXXXA", hide(account))
        localStorage.setItem("XXXXP", hide(password))
      } else {
        localStorage.removeItem("XXXXA")
        localStorage.removeItem("XXXXP")
      }
    }
  },
  created() {
    let acc = localStorage.getItem("XXXXA")
    let pwd = localStorage.getItem("XXXXP")
    if (acc != null && pwd != null) {
      this.loginForm.account = show(acc)
      this.loginForm.password = show(pwd)
      this.loginForm.checked = true
    }
  }
}
</script>

<style scoped lang="less">
@theme-color: #a0d8ef;
@bg-color: #f5f7fa;

.login {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url(../assets/img/backgroud.jpg);
  background-size: cover;
  background-position: center;
  display: flex;
  justify-content: center;
  align-items: center;

  .login-card {
    width: 420px;
    background-color: rgba(255, 255, 255, 0.95);
    border-radius: 8px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);

    h3 {
      text-align: center;
      margin-bottom: 30px;
      color: #303133;
      font-size: 24px;
    }

    .el-form-item {
      margin-bottom: 20px;
    }
  }
}
</style>
